﻿using System;
using System.Collections.Generic;
using HYCodeBase.XmlUtility;

namespace HYCodeBase.WinForm.WinFormRemindSuit
{
	internal static class XmlNodeInformationTransformer
	{
		internal static XmlNodeInformationBean FormBeanToXmlNodeBean(FormInformationBean inputBean)
		{
			XmlNodeInformationBean xmlNodeInfoBean = XmlNodeInformationTransformer.GetXmlNodeInfoBean(inputBean);
			xmlNodeInfoBean.LstSubNodes = new List<XmlNodeInformationBean>();
			foreach (ControlInformationBean inputBean2 in inputBean.LstAllControls)
			{
				xmlNodeInfoBean.LstSubNodes.Add(XmlNodeInformationTransformer.GetXmlNodeInfoBean(inputBean2));
			}
			return xmlNodeInfoBean;
		}

		internal static List<KeyValuePair<FormInformationBean, string>> ReadAllFormBeanFromXmlNodeBean(XmlNodeInformationBean inputBean)
		{
			List<KeyValuePair<FormInformationBean, string>> list = new List<KeyValuePair<FormInformationBean, string>>();
			FormInformationBean formInformationBean = null;
			string text = null;
			string value = string.Empty;
			try
			{
				if (inputBean.StrNodeName.Equals(XmlNodeInformationTransformer.m_strForm))
				{
					List<AttributeInformationBean> attributes = XmlNodeInformationTransformer.GetAttributes(inputBean, ref text);
					if (string.IsNullOrEmpty(text))
					{
						text = XmlNodeInformationTransformer.m_strForm;
					}
					value = inputBean.StrInnerText;
					formInformationBean = new FormInformationBean(text, attributes);
					foreach (XmlNodeInformationBean oneNode in inputBean.LstSubNodes)
					{
						ControlInformationBean controlInformationBean = XmlNodeInformationTransformer.TransformToControlInformationBean(oneNode);
						if (controlInformationBean != null)
						{
							formInformationBean.AddOneControlBean(controlInformationBean);
						}
					}
				}
			}
			catch (Exception)
			{
				formInformationBean = null;
			}
			if (formInformationBean != null)
			{
				list.Add(new KeyValuePair<FormInformationBean, string>(formInformationBean, value));
			}
			foreach (XmlNodeInformationBean inputBean2 in inputBean.LstSubNodes)
			{
				list.AddRange(XmlNodeInformationTransformer.ReadAllFormBeanFromXmlNodeBean(inputBean2));
			}
			return list;
		}

		private static ControlInformationBean TransformToControlInformationBean(XmlNodeInformationBean oneNode)
		{
			string text = null;
			List<AttributeInformationBean> lstInputAttribute = new List<AttributeInformationBean>();
			ControlInformationBean result = null;
			try
			{
				if (oneNode.StrNodeName.Equals(XmlNodeInformationTransformer.m_strControl))
				{
					lstInputAttribute = XmlNodeInformationTransformer.GetAttributes(oneNode, ref text);
					if (string.IsNullOrEmpty(text))
					{
						text = XmlNodeInformationTransformer.m_strControl;
					}
					result = new ControlInformationBean(text, lstInputAttribute);
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private static List<AttributeInformationBean> GetAttributes(XmlNodeInformationBean inputBean, ref string findNameValue)
		{
			findNameValue = null;
			List<AttributeInformationBean> list = new List<AttributeInformationBean>();
			foreach (KeyValuePair<string, string> keyValuePair in inputBean.LstKvpAttribute)
			{
				AttributeInformationBean item = new AttributeInformationBean(keyValuePair.Key, keyValuePair.Value);
				list.Add(item);
				if (keyValuePair.Key.Equals(BaseControlTransFormer.StrName))
				{
					findNameValue = keyValuePair.Value;
				}
			}
			return list;
		}

		private static XmlNodeInformationBean GetXmlNodeInfoBean(ControlInformationBean inputBean)
		{
			XmlNodeInformationBean xmlNodeInformationBean = new XmlNodeInformationBean();
			List<AttributeInformationBean> lstAttributions = inputBean.LstAttributions;
			xmlNodeInformationBean.LstKvpAttribute = new List<KeyValuePair<string, string>>();
			foreach (AttributeInformationBean attributeInformationBean in lstAttributions)
			{
				KeyValuePair<string, string> item = new KeyValuePair<string, string>(attributeInformationBean.StrAttributeName, attributeInformationBean.StrValueString);
				xmlNodeInformationBean.LstKvpAttribute.Add(item);
			}
			if (inputBean is FormInformationBean)
			{
				xmlNodeInformationBean.StrNodeName = XmlNodeInformationTransformer.m_strForm;
			}
			else
			{
				xmlNodeInformationBean.StrNodeName = XmlNodeInformationTransformer.m_strControl;
			}
			return xmlNodeInformationBean;
		}

		private static string m_strForm = "Form";

		private static string m_strControl = "Control";
	}
}
